/**
 * <p>
 * 版权所有：中企动力
 * </p>
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 * </p>
 */
package com.ce.pms.renovation.web;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.renovation.service.IInspectionService;
import com.ce.pms.renovation.vo.FirstInspectionVO;
import com.ce.pms.renovation.vo.InspectionRequisitionVO;
import com.ce.pms.renovation.vo.InspectionViolationVO;
import com.ce.pms.renovation.vo.ReinspectionVO;
import com.ce.util.DateUtil;
import com.sitechasia.webx.core.annotation.Out;
import com.sitechasia.webx.core.annotation.Read;

/**
 * 初验Action
 *
 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
 * @Created 2008-12-26
 */

public class FirstInspectionAction extends PmsBaseAction {
	private final String		EDIT_PAGE	= "firstInspectionEdit";

	private final String		VIOL_PAGE	= "inspectionViolation";

	private IInspectionService	inspectionService;

	/**
	 * 添加初验
	 *
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-26
	 */
	public void editFirstInspection(@Read FirstInspectionVO firstInspectionVO) {
		firstInspectionVO.setInspectionDate(DateUtil.convertStrToDate(
				firstInspectionVO.getInspectionDateStr(), "yyyy-MM-dd HH时"));
		try {
			if (firstInspectionVO.getKeyword() == null) {
				firstInspectionVO.setCreator(this.getUserId());
				firstInspectionVO = this.inspectionService
						.firstInspection(firstInspectionVO);
			} else {
				firstInspectionVO.setModifier(this.getUserId());
				firstInspectionVO = this.inspectionService
						.firstInspection(firstInspectionVO);
			}
			// this.setAttribute("inspectionRequisitionId",
			// firstInspectionVO.getInspectionRequisitionId(), Scope.SESSION);
			this.renderText(firstInspectionVO.getKeyword().toString());
		} catch (RuntimeException re) {
			throw re;
		}
	}

	/**
	 * 显示初验结果
	 *
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-9
	 */
	public String showDetailPage(
			@Read(key = "inspectionRequisitionId") Long inspectionRequisitionId,
			@Read(key = "isReinspection")@Out(key = "isReinspection") boolean isReinspection) {

		if (isReinspection) {
			ReinspectionVO reinspectionVO = this.inspectionService
					.getReinspection(inspectionRequisitionId);
			if (reinspectionVO != null) {
				InspectionViolationVO inspectionViolationVO = this.inspectionService
						.getInspectionViolation(reinspectionVO.getKeyword());

				this.setAttribute("firstInspectionVO", reinspectionVO);
				this.setAttribute("inspectionViolationVO",
						inspectionViolationVO);
			}

		} else {
			FirstInspectionVO firstInspectionVO = this.inspectionService
					.getFirstInspection(inspectionRequisitionId);
			if (firstInspectionVO != null) {
				InspectionViolationVO inspectionViolationVO = this.inspectionService
						.getInspectionViolation(firstInspectionVO.getKeyword());

				this.setAttribute("firstInspectionVO", firstInspectionVO);
				this.setAttribute("inspectionViolationVO",
						inspectionViolationVO);
			}
		}
		return "firstInspectionDetail";
	}

	/**
	 * 初验或复验添加页面
	 *
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-26
	 */
	public String editPage(
			@Read(key = "inspectionRequisitionId") @Out(key = "inspectionRequisitionId") Long inspectionRequisitionId,
			@Read(key = "reinspectionId") @Out(key = "reinspectionId") Long reinspectionId,
			@Read(key = "isReinspection") @Out(key = "isReinspection") String isReinspection) {
		if (!"1".equals(isReinspection)) { // 初验
			FirstInspectionVO firstInspectionVO = this.inspectionService
					.getFirstInspection(inspectionRequisitionId);
			if (firstInspectionVO != null) {
				this.setAttribute("firstInspectionVO", firstInspectionVO);
			}
		} else {// 复验
			if (reinspectionId != null) {
				ReinspectionVO reinspectionVO = this.inspectionService
						.getReinspection(reinspectionId);
				this.setAttribute("firstInspectionVO", reinspectionVO);
			}
		}
		return this.EDIT_PAGE;
	}

	/**
	 * 初验或复验违规登记添加或编辑
	 *
	 * @param inspectionRequisitionId 验收申请ID
	 * @param InspectionId 初验或复验ID
	 * @param isReinspection 是否是复验
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-6
	 */
	public String initInspectionViolation(
			@Read(key = "inspectionRequisitionId") @Out(key = "inspectionRequisitionId") Long inspectionRequisitionId,
			@Read(key = "inspectionId") @Out(key = "inspectionId") Long inspectionId,
			@Read(key = "isReinspection") @Out(key = "isReinspection") String isReinspection) {
		if ("1".equals(isReinspection)) {// 复验
			InspectionViolationVO inspectionViolationVO = this.inspectionService
					.getInspectionViolation(inspectionId);

			this.setAttribute("inspectionViolationVO", inspectionViolationVO);
		} else { // 初验
			InspectionViolationVO inspectionViolationVO = this.inspectionService
					.getInspectionViolation(inspectionId);

			this.setAttribute("inspectionViolationVO", inspectionViolationVO);
		}
		InspectionRequisitionVO inspectionRequisitionVO = this.inspectionService
				.getInspectionRequisition(inspectionRequisitionId);
		this.setAttribute("inspectionRequisitionVO", inspectionRequisitionVO);

		return VIOL_PAGE;
	}

	/**
	 * 添加或编辑违规登记
	 *
	 * @param inspectionViolationVO
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-5
	 */
	public void addInspectionViolation(
			@Read InspectionViolationVO inspectionViolationVO) {
		try {
			if (inspectionViolationVO.getKeyword() == null) {
				inspectionViolationVO.setCreator(getUserId());
				this.inspectionService
						.registerInspectionViolation(inspectionViolationVO);
			} else {
				inspectionViolationVO.setModifier(getUserId());
				this.inspectionService
						.registerInspectionViolation(inspectionViolationVO);
			}
			this.renderText("true");
		} catch (RuntimeException re) {
			throw re;
		}
	}

	/**
	 * @param inspectionService the inspectionService to set
	 */
	public void setInspectionService(IInspectionService inspectionService) {
		this.inspectionService = inspectionService;
	}
}
